import pygame


class MyPlane:
    def __init__(self, gm):
        """初始化飞机并设置初始位置"""
        self.screen = gm.screen
        self.screen_rect = gm.screen.get_rect()
        self.settings = gm.settings

        self.image = pygame.image.load("./images/my_plane.gif")
        self.rect = self.image.get_rect()

        # 放在屏幕底部中央
        self.rect.midbottom = self.screen_rect.midbottom

        # 在飞机的属性x中存储一个浮点数
        self.x = float(self.rect.x)
        # 移动标志
        self.moving_right = False
        self.moving_left = False

    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.settings.my_plane_move_speed
        if self.moving_left and self.rect.left > 0:
            self.x -= self.settings.my_plane_move_speed
        self.rect.x = self.x

    def blitme(self):
        """在指定位置绘制"""
        self.screen.blit(self.image, self.rect)

    def center_my_plane(self):
        """将飞机放在屏幕底部中央"""
        self.rect.midbottom = self.screen_rect.midbottom
        self.x = float(self.rect.x)
